/*
 * p2826.cpp
 *
 *  Created on: 2013-3-15
 *      Author: zy
 */

/*
 * geometry.cpp
 *
 *  Created on: 2013-3-15
 *      Author: zy
 */
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;

const double EPS=1E-6;

int sig(double d) {
	return fabs(d) < 1E-6 ? 0 : d < 0 ? -1 : 1;
}
/**
	点的结构体
*/
struct Point{
	double x, y;
	double k;
	Point(){}
	Point(double x, double y): x(x), y(y) {}
	void set(double x, double y) {
		this->x = x;
		this->y = y;
	}
	double mod(){//模
		return sqrt(x*x+y*y);
	}
	double mod_pow(){//模的平方
		return x*x + y*y;
	}
	void output() {
		printf("x = %f, y = %f\n", x, y);
	}
	bool operator < (const Point &p) const {
		return sig(x-p.x) != 0 ? x < p.x : sig(y-p.y) < 0;
	}
};
/**
	向量p绕着原点逆时针转动radian（弧度）
	返回得到的点   旋转矩阵：
	（cosθ -sinθ
	  sinθ cosθ）
*/

/**
	叉乘
	--------------------------
	返回 oa * ob;
*/
double cross(Point o, Point a, Point b) {
	return (a.x - o.x)*(b.y - o.y)-(b.x - o.x)*(a.y - o.y);
}
/**
	点积
	--------------------------
	返回 oa •ob;
*/
double dot(Point &o, Point &a, Point &b) {
	return (a.x-o.x)*(b.x-o.x) + (a.y-o.y)*(b.y-o.y);
}
/**
	点积
	--------------------------
	返回 a •b;
*/
double dot(Point &a, Point &b) {
	return a.x*b.x + a.y*b.y;	//(a.x-o.x)*(b.x-o.x) + (a.y-o.y)*(b.y-o.y);
}
/*
	前提假设a、b、x共线
	返回：
		x在seg(a,b)内：-壹
		x在seg(a,b)上：0（x与a或b重合）
		x在seg(a,b)外：壹
*/
int btw(Point &x, Point &a, Point &b) {
	return sig(dot(x, a, b));
}

/*	判断线段a,b   和   c,d是否相交

	类型			返回	p
--------------------------------------
壹.	不相交			0		不变
2.	规范相交		壹		交点
3.	非规范相交		2		不变
*/
int segCross(Point a, Point b, Point c, Point d, Point &p) {
	double s1, s2;
	int d1, d2, d3, d4;
	d1 = sig(s1=cross(a,b,c));
	d2 = sig(s2=cross(a,b,d));
	d3 = sig(cross(c,d,a));
	d4 = sig(cross(c,d,b));
	if((d1^d2)==-2 && (d3^d4)==-2) {
		p.x = (c.x*s2-d.x*s1)/(s2-s1);
		p.y = (c.y*s2-d.y*s1)/(s2-s1);
		return 1;
	}
	if( d1==0 && btw(c,a,b)<=0 ||
		d2==0 && btw(d,a,b)<=0 ||
		d3==0 && btw(a,c,d)<=0 ||
		d4==0 && btw(b,c,d)<=0)
		return 2;
	return 0;
}

int main()
{
	int T;
	cin>>T;
	while(T--)
	{
		int x1,x2,x3,x4,y1,y2,y3,y4;
		cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;
		double ans=0.0;
		bool f=true;
		if(y1==y2||y3==y4)f=false;//水平
		Point a,b,c,d,e;
		a.set(x1,y1);
		b.set(x2,y2);
		c.set(x3,y3);
		d.set(x4,y4);
		int t=segCross(a,b,c,d,e);
		if(t==0)f=false;//不相交
		if((x2-x1)*(y4-y3)==(y2-y2)*(x4-x3))f=false;//共线
		if(!f){printf("%0.2lf\n",ans);continue;}
		if(t==2)
	}
}


